import scrapy
from scrapy.selector import Selector
from ..items import NewsItem
from ..spiders import utils_crawler

source = u'宣讲家网'


class XuanjiajiangSpider(scrapy.Spider):
    name = 'xuanjiajiang'
    start_urls = [
        "http://www.71.cn/acastudies/impremarks/speech/",
    ]

    def parse(self, response):
        item_urls = []
        sel = Selector(response)
        hrefs = sel.xpath('//div[@class="articlelist_title"]//a/@href').extract()
        for href in hrefs:
            if href.find('http://www.71.cn/2016') != -1:
                item_urls.append(href)
        for item_url in item_urls:
            yield scrapy.Request(url=item_url, callback=self.parse_details)

    def parse_details(self, response):
        item = NewsItem()
        sel = Selector(response)

        item['title'] = sel.xpath('//h1[@class="article-title"]/text()').extract()[0].strip()
        item['href'] = response.url
        item['time'] = sel.xpath('//span[@class="date"]/text()').extract()[0].strip() + ':00'
        item['content'] = utils_crawler.deal_content(sel.xpath('//div[@class="inner"]//p/text()').extract())
        item['source'] = source
        item['image_urls'] = sel.xpath('//div[@class="inner"]//img/@src').extract()

        return item
